<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>登录</title>
        <!-- 样 式 文 件 -->
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/pages/login.css" />
    </head>
    <!-- 代 码 结 构 -->
    <body background="/app/admin/admin/images/background.svg" style="background-size: cover;">
        <form class="layui-form">
            <div class="layui-form-item">
                <img class="logo" src="<?=$logo?>" />
                <div class="title pear-text"><?=$title?></div>
            </div>
            <div class="layui-form-item">
                <input lay-verify="required" hover class="layui-input" type="text" name="username" value="" placeholder="用户名1" />
            </div>
            <div class="layui-form-item">
                <input lay-verify="required" hover class="layui-input" type="password" name="password" value="" placeholder="密码1" />
            </div>
            <div class="layui-form-item">
                <input hover  lay-verify="required" class="code layui-input layui-input-inline" name="captcha" placeholder="验证码1" />
                <img class="codeImage" width="120px"/>
            </div>
            <div class="layui-form-item">
                <button type="submit" class="pear-btn pear-btn-primary login" lay-submit lay-filter="login">
                    登 入
                </button>
            </div>
        </form>
        <script>
            var color = localStorage.getItem("theme-color-color");
            var second = localStorage.getItem("theme-color-second");
            if (!color || !second) {
                localStorage.setItem("theme-color-color", "#2d8cf0");
                localStorage.setItem("theme-color-second", "#ecf5ff");
            }
        </script>
        <!-- 资 源 引 入 -->
        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script>
            layui.use(['form', 'button', 'popup', 'layer', 'theme', 'admin'], function() {
                var $ = layui.$, layer = layui.layer, form = layui.form;
                
                // 获取URL中的redirect参数
                function getRedirectUrl() {
                    var urlParams = new URLSearchParams(window.location.search);
                    return urlParams.get('redirect');
                }
                
                function switchCaptcha() {
                    $('.codeImage').attr("src", "/app/ky_shouyin/admin/account/captcha/login?v=" + new Date().getTime());
                }
                switchCaptcha();
                
                // 登 录 提 交
                form.on('submit(login)', function (data) {
                    layer.load();
                    $.ajax({
                        url: '/app/ky_shouyin/admin/account/login',
                        type: "POST",
                        data: data.field,
                        success: function (res) {
                            layer.closeAll('loading');
                            if(res.code === 0) {
                                layer.msg('登录成功', {icon: 1});
                                // 检查是否有重定向地址
                                var urlRedirect = getRedirectUrl();
                                var defaultRedirect = res.data && res.data.redirect_url;
                                var redirectUrl = urlRedirect || defaultRedirect;
                                console.log('Redirect URL:', redirectUrl);
                                
                                setTimeout(function() {
                                    if (redirectUrl) {
                                        // 始终使用临时页面进行跳转
                                        var tempUrl = '/app/ky_shouyin/front/temporary?redirect=' + 
                                            encodeURIComponent(redirectUrl);
                                        console.log('Temp URL:', tempUrl);
                                        window.location.href = tempUrl;
                                    } else {
                                        layer.msg('跳转地址无效', {icon: 2});
                                    }
                                }, 1000);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                                switchCaptcha();
                            }
                        }
                    });
                    return false;
                });
                $('.codeImage').on('click', function () {
                    switchCaptcha();
                });
            })
        </script>
    </body>
</html>
